<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */
// ------------------------------------------------------------------------

/**
 * Parser Class
 *
 * @package		CodeIgniter
 * @subpackage	Libraries
 * @category	Parser
 * @author		ExpressionEngine Dev Team
 * @link		http://codeigniter.com/user_guide/libraries/parser.html
 */
class CMS_Parser extends CI_Parser {

    private $_js = array();
    private $_css = array();

    function __construct() {
        
    }

    /**
     *  Parse a template
     *
     * Parses pseudo-variables contained in the specified template view,
     * replacing them with the data in the second param
     *
     * @access	public
     * @param	string
     * @param	array
     * @param	bool
     * @return	string
     */
    public function parse_cms($template, $data, $return = FALSE) {

        $CI = &get_instance();
        $CI->load->library('permissions');
        //$this->permission->_checked();
        $template = $CI->load->view($template, $data, TRUE);
        $CI->lang->load('defaut_view');

        $d = array();
        $d = $CI->lang->multi_line($d);
        $d['css'] = $this->_get_css();
        $d['js'] = $this->_get_js();
        $d['base_url'] = base_url();
        $d['base_cms'] = base_url() . base_cms();
        $d['base_site'] = base_url() . base_site();
        $d['base_template'] = base_pasta_templates() . '/' . base_template_cms();
        $d['base_template_site'] = base_pasta_templates() . '/' . base_template_site();
        $d['idioma_js'] = $CI->config->item('language');
        $d['imagens_imagem_gravar'] = $CI->permissions->get_url('imagens_imagem_gravar');
        $d1['include_header_view'] = $this->parse('include_header_view', $d, TRUE);
        $d1['include_footer_view'] = $this->parse('include_footer_view', $d, TRUE);
        $d1['include_body_header_view'] = $this->_header_extra($d);
        $d1['include_body_menu_left_view'] = $this->_monta_meu_lateral($d);

        foreach ($data as $k => $v) {
            $d[$k] = $v;
        }
        $da = array();
        $da = array_merge($d, $d1);
        return $this->_parse($template, $da, $return);
    }

    function _header_extra($v_dados = array()) {
        $CI = &get_instance();
        $CI->load->library('permissions');
        //$CI->load->config('config');
        $v_dados['login_auth_logout'] = $CI->permissions->get_url('login_auth_logout');
        $v_dados['settings_settings_index'] = $CI->permissions->get_url('settings_settings_index');
        $v_dados['user_index'] = $CI->permissions->get_url('user_user_index');
        $v_dados['conteudo_idioma_db'] = $CI->config->item('language');
        return $this->parse('include_header_extra_view', $v_dados, TRUE);
    }

    function _monta_meu_lateral($v_dados = array()) {

        $CI = &get_instance();
        $CI->load->library('permissions');

        if ($CI->uri->segment(1) == 'cms') {
            $link_active = $CI->uri->segment(2) . '/' . $CI->uri->segment(3) . '/' . $CI->uri->segment(4);
            $link_a = $CI->uri->segment(2);
        } else {
            $link_active = $CI->uri->segment(1) . '/' . $CI->uri->segment(2) . '/' . $CI->uri->segment(3);
            $link_a = $CI->uri->segment(1);
        }
        $b_consultar = $CI->permissions->monta_menu();
        $v_dados['menu'] = array();
        if ($b_consultar != null) {
            foreach ($b_consultar as $k => $v) {

                //$CI->lang->load((String) trim(strtolower($v->nome)).'/menu_' . trim(strtolower($v->nome)));
                $CI->load->add_module((String) trim(strtolower($v->nome)));
                $CI->lang->load('menu_' . trim(strtolower($v->nome)));

                if (!isset($v_dados['menu'][$v->nome])) {
                    $v_dados['menu'][$v->nome]['nome'] = $CI->lang->line('lang_' . $v->nome);
                    $v_dados['menu'][$v->nome]['pai'] = $v->pai;
                    $v_dados['menu'][$v->nome]['function'] = $CI->lang->line('lang_' . $v->function);
                    $v_dados['menu'][$v->nome]['url'] = $v->url;
                }

                if (!isset($v_dados['menu'][$v->nome]['filho'])) {
                    $v_dados['menu'][$v->nome]['filho'] = array();
                    $v_dados['menu'][$v->nome]['a_active'] = '';
                    $v_dados['menu'][$v->nome]['ul_active'] = '';
                }

                $v_dados['menu'][$v->nome]['filho'][$v->function]['nome_filho'] = $v->nome;
                $v_dados['menu'][$v->nome]['filho'][$v->function]['pai_filho'] = $v->pai;
                $v_dados['menu'][$v->nome]['filho'][$v->function]['function_filho'] = $CI->lang->line('lang_' . $v->function);
                $v_dados['menu'][$v->nome]['filho'][$v->function]['url_filho'] = $v->url;
                $v_dados['menu'][$v->nome]['filho'][$v->function]['active'] = ($link_active == $v->url ? 'class="active"' : '');

                $v_dados['menu'][$v->nome]['a_active'] = ($link_a == $v->nome ? 'class="active"' : '');
                $v_dados['menu'][$v->nome]['ul_active'] = ($link_a == $v->nome ? 'style="display: block; "' : '');
            }
        }
        return $this->parse('include_menu_left_view', $v_dados, TRUE);
    }

    public function parse_site($template, $data, $return = FALSE) {
        $CI = &get_instance();
        //pre($data);
        $template = $CI->load->view($template, $data, TRUE);
        $CI->lang->load('defaut_view');
        $data['base_url'] = base_url();
        $data['base_cms'] = base_cms();
        $data['base_site'] = base_site();
        $data['base_template'] = base_pasta_templates() . '/' . base_template_cms();
        $data['base_template_site'] = base_pasta_templates() . '/' . base_template_site();
        $data['include_header'] = $this->parse('includeHeader', $data, TRUE);
        $data['links_conteudo'] = $this->_monta_rodape_motocerto();
        $data['include_footer'] = $this->parse('includeFooter', $data, TRUE);
        $data['include_banner_600'] = $this->parse('includeBanner600', $data, TRUE);

        return $this->_parse($template, $data, $return);
    }

    public function parse_site2($template, $data, $return = FALSE) {
        $CI = &get_instance();
        $template = $CI->load->view($template, $data, TRUE);
        $CI->lang->load('defaut_view');
        /* $d = array();
          $d = $CI->lang->multi_line($d);
          $d['base_url'] = base_url();
          $d['base_cms'] = base_cms();
          $d['base_site'] = base_site();
          $d['base_template'] = base_template();
          $d['base_template_site'] = base_template_site();
          //$d['imagens_imagem_gravar'] = $CI->permissions->get_url('imagens_imagem_gravar');
          $d1['includeCSS'] = $this->parse('includeCSS', $d, TRUE);
          $d1['includeJS'] = $this->parse('includeJS', $d, TRUE);
          $d1['include_footer'] = $this->parse('includeFooter', $d, TRUE);

          //$d['menu_categorias'] = $this->_menu_categoria();

          $d1['include_menu'] = $this->parse('includeMenu', $d, TRUE);
          //$d1['include_body_menu_left_view'] = $this->_monta_meu_lateral($d);
          foreach ($data as $k => $v) {
          $d[$k] = $v;
          }
          $da = array();
          $da = array_merge($d, $d1); */

//pre($da);
        /*
          ["base_url"]=>  string(24) "http://cliente.srv.br/a/"
          ["base_cms"]=>  string(28) "http://cliente.srv.br/a/cms/"
          ["base_site"]=>  string(32) "http://cliente.srv.br/a/manuela/"
          ["base_template"]=>  string(14) "templates/cms/"
          ["base_template_site"]=>  string(18) "templates/manuela/"
         */
        //pre($data);
        $data['base_url'] = base_url();
        $data['base_cms'] = base_cms();
        $data['base_site'] = base_site();
        $data['base_template'] = base_template();
        $data['base_template_site'] = base_template_site();
//$d['imagens_imagem_gravar'] = $CI->permissions->get_url('imagens_imagem_gravar');
        $data['includeCSS'] = $this->parse('includeCSS', $data, TRUE);
        $data['includeJS'] = $this->parse('includeJS', $data, TRUE);
        $data['include_footer'] = $this->parse('includeFooter', $data, TRUE);

//$d['menu_categorias'] = $this->_menu_categoria();
        $data['include_menu'] = $this->parse('includeMenu', $data, TRUE);
        $data['css'] = $this->_get_css();
        $data['js'] = $this->_get_js();

        pre($data);

        return $this->_parse($template, $data, $return);
    }

    public function add_css($css = '') {
        if ($css != '') {
            if (is_array($css)) {
                foreach ($css as $value) {
                    $this->_set_css($value);
                }
            } else {
                $this->_set_css($css);
            }
        }
    }

    private function _set_css($css) {
        $this->_css[md5($css)] = '<link rel="stylesheet" href="css/' . $css . '.css">';
        $this->_css = array_unique($this->_css);
    }

    private function _get_css() {
        return implode("\n", $this->_css);
    }

    function add_js($js, $ultimo = FALSE) {
        if (is_array($js)) {
            foreach ($js as $value) {
                $this->_set_js($value, $ultimo);
            }
        } else {
            $this->_set_js($js, $ultimo);
        }
    }

    private function _set_js($js, $ultimo = FALSE) {
        if ($ultimo == FALSE) {
            $this->_js['inicio'][md5($js)] = '<script type="text/javascript" src="js/' . $js . '.js"></script>';
        } else {
            $this->_js['fim'][md5($js)] = '<script type="text/javascript" src="js/' . $js . '.js"></script>';
        }
    }

    private function _get_js() {
        $js = '';
        if (isset($this->_js['inicio']) && count($this->_js['inicio']) > 0) {
            $js .= implode("\n", $this->_js['inicio']);
        }

        if (isset($this->_js['fim']) && count($this->_js['fim']) > 0) {
            $js .= "\n" . implode("\n", $this->_js['fim']);
        }
        return $js;
    }

    function _monta_rodape_motocerto() {
        $v_dados = array();
        $CI = &get_instance();
        $CI->load->model('conteudo/conteudo_model');
        $b_consultar = $CI->conteudo_model->consultar_tipos();
        if ($b_consultar != NULL) {
            foreach ($b_consultar as $k => $v) {
                $v_dados[$v->conteudo_categoria_id]['tipo'][$k]['conteudo_titulo'] = anchor(base_url().'blog/' . $v->id . '/' . url_title($v->titulo, '_', TRUE), $v->titulo);
                $v_dados[$v->conteudo_categoria_id]['conteudo_tipo'] = '<a>'.$v->cc_nome.'</a>';
            }
        }
        return $v_dados;
        //return $this->parse('include_menu_left_view', $v_dados, TRUE);
    }

}

// END Parser Class

/* End of file Parser.php */
/* Location: ./system/libraries/Parser.php */
